"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : 将系统权限login授权给用户
Description :
        1.创建用户赋予login权限并且查看是否有登录权限；
        2.退出当前数据库，用新建用户连接数据库；
        3.查看用户是否有登录权限；
        4.清理环境；
Expect      :
        1.创建用户成功,有登录权限；
        2.连接成功；
        3.有登陆权限；
        4.清理环境成功；
History     :
"""

import sys
import unittest
from yat.test import Node
from yat.test import macro
sys.path.append(sys.path[0] + "/../")
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

logger = Logger()

class Tools(unittest.TestCase):
    def setUp(self):
        logger.info('--------------Opengauss_Function_DCL_Grant_Case0021.py start-------------------')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.Constant = Constant()

    def test_server_tools(self):
        logger.info('------------------创建用户赋予login权限------------------')
        self.user = 'p_user01'
        sql_cmd1 =f'''
                    
                    drop role if exists {self.user} cascade;
                    create role {self.user} with login identified by '{macro.COMMON_PASSWD}';
                    select rolcanlogin from pg_roles where rolname = '{self.user}';
                    '''
        msg1 = self.sh_primary.execut_db_sql(sql_cmd1)
        logger.info(msg1)
        self.assertIn(self.Constant.CREATE_ROLE_SUCCESS_MSG, msg1)
        self.assertIn('t', msg1)

        logger.info('------------------退出当前数据库，用新建用户连接数据库,查看用户是否有登录权限------------------')
        sql_cmd2 = f'''select rolcanlogin from pg_roles where rolname = '{self.user}';'''
        execute_cmd2 = f'''
                     source {self.DB_ENV_PATH}; 
                     gsql -d {self.userNode.db_name} -U {self.user} -W {macro.COMMON_PASSWD} -p {self.userNode.db_port} -c "{sql_cmd2}"
                    '''
        logger.info(execute_cmd2)
        msg2 = self.userNode.sh(execute_cmd2).result()
        logger.info(msg2)
        self.assertIn('t', msg2)

    def tearDown(self):
        logger.info('--------------清理环境-------------------')
        sql_cmd3 = f'''
                    drop user {self.user} cascade;
                    '''
        msg3 = self.sh_primary.execut_db_sql(sql_cmd3)
        logger.info(msg3)
        logger.info('------------------Opengauss_Function_DCL_Grant_Case0021.py finish------------------')
